package com.mbta.tracker;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.database.Cursor;

/**
 * Test comment
 * @author jdelfino
 */
public class RouteInfo {
	private static String ROUTE_ELEMENT_NAME = new String("route");
	private static String DIRECTION_ELEMENT_NAME = new String("direction");
	private static String STOP_ELEMENT_NAME = new String("stop");
	
	private static String TAG_ATTRIBUTE_NAME = new String("tag");
	private static String LAT_ATTRIBUTE_NAME = new String("lat");
	private static String LONG_ATTRIBUTE_NAME = new String("lon");
	private static String TITLE_ATTRIBUTE_NAME = new String("title");

	
	public static List<String> getRouteList() {
		HttpClient client = new DefaultHttpClient();
    	HttpHost host = new HttpHost("webservices.nextbus.com");
    	HttpGet request = new HttpGet("/service/publicXMLFeed?command=routeList&a=mbta");
    	HttpResponse response;
    	try {
    		response = client.execute(host, request);
    	} catch (IOException e) {
    		return null;
    	} 

    	List<String> rval = new ArrayList<String>();
    	
    	try {
    		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    		XmlPullParser xpp = factory.newPullParser();

    		xpp.setInput(response.getEntity().getContent(), null);

    		int eventType = xpp.getEventType();
    		while (eventType != XmlPullParser.END_DOCUMENT) {
    			if(eventType == XmlPullParser.START_TAG && xpp.getName().equals(ROUTE_ELEMENT_NAME)) {
    				assert(xpp.getAttributeValue(null, TAG_ATTRIBUTE_NAME) != null);
    				rval.add(xpp.getAttributeValue(null, TAG_ATTRIBUTE_NAME));
    			}
    			eventType = xpp.next();
    		}
    	} catch(XmlPullParserException e) {
    		return null;
    	} catch (IOException e) {
    		return null;
    	}
	
        return rval;
	}
	
	static List<Integer> getPredictedTimes(String stopId) {
		return new ArrayList<Integer>();
	}
	
	static List<Prediction> getPredictedTimes(Cursor stopIds){
		return new ArrayList<Prediction>();
	}
}
